package com.zhangtai.modules.service.impl.system;


import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.zhangtai.modules.dao.AuthorityLogDao;
import com.zhangtai.modules.entity.system.AuthorityLogEntity;
import com.zhangtai.modules.service.system.AuthorityLogService;
import com.zhangtai.utils.PageResult;
import com.zhangtai.utils.R;
import org.apache.commons.lang3.StringUtils;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.util.CollectionUtils;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import java.util.List;


@RestController
public class AuthorityLogServiceImpl extends ServiceImpl<AuthorityLogDao, AuthorityLogEntity> implements AuthorityLogService {

    @Override
    public PageResult queryPage(  @RequestParam(value = "orgIds",required = false) List<Long> orgIds,
                                @RequestParam(value = "updateUserName", required = false) String updateUserName,
                                @RequestParam(value = "jobName", required = false) String jobName,
                                @RequestParam(value = "method", required = false) String method,
                                @RequestParam(value = "type", required = false) String type,
                                @RequestParam(value = "startTime", required = false) String startTime,
                                @RequestParam(value = "endTime", required = false) String endTime,
                                  @RequestParam(value = "remark", required = false) String remark,
                                  @RequestParam(value = "params", required = false) String params,
                                @RequestParam(value = "pageNum", defaultValue = "1") Integer pageNum,
                                @RequestParam(value = "pageSize", defaultValue = "10") Integer pageSize) {

        IPage<AuthorityLogEntity> page = this.page(
                new Page<AuthorityLogEntity>(pageNum,pageSize),
                new QueryWrapper<AuthorityLogEntity>()
                        .in(!CollectionUtils.isEmpty(orgIds),"org_id",orgIds)
                        .eq(CollectionUtils.isEmpty(orgIds),"org_id",null)
                        .like(!StringUtils.isEmpty(updateUserName),"update_user_name",updateUserName)
                        .like(!StringUtils.isEmpty(jobName),"job_name",jobName)
                        .like(!StringUtils.isEmpty(method),"method",method)
                        .eq(!StringUtils.isEmpty(type),"type",type)
                        .eq(!StringUtils.isEmpty(remark),"remark",remark)
                        .eq(!StringUtils.isEmpty(params),"params",params)
                        .ge(!StringUtils.isEmpty(startTime),"update_time",startTime)
                        .le(!StringUtils.isEmpty(endTime),"update_time",endTime)
                        .eq("is_delete",0).orderByDesc("update_time")
        );

        return new PageResult(page);
    }

    @Override
    public PageResult queryPageBYOid(@RequestParam(value = "oid", required = false) String oid,
                              @RequestParam(value = "pageNum", defaultValue = "1") Integer pageNum,
                              @RequestParam(value = "pageSize", defaultValue = "10") Integer pageSize){

        IPage<AuthorityLogEntity> page = this.page(
                new Page<AuthorityLogEntity>(pageNum,pageSize),
                new QueryWrapper<AuthorityLogEntity>()
                        .eq(!StringUtils.isEmpty(oid),"oid",oid)
                        .eq("type", 1)
                        .eq("is_delete",0).orderByDesc("update_time")
        );

        return new PageResult(page);
    }

    @Override
    public AuthorityLogEntity getByIdE(Long id) {
            AuthorityLogEntity one = this.getOne(new QueryWrapper<AuthorityLogEntity>().eq("id", id).eq("is_delete", 0));

        return one;
    }

    @Override
    public R saveE(AuthorityLogEntity entity) {
        boolean b = this.save(entity);
        if (!b){
            return R.error();
        }
        return R.ok();

    }

    @Override
    public R updateByIdE(AuthorityLogEntity entity) {
        boolean b = this.updateById(entity);
        if (!b){
            return R.error();
        }
        return R.ok();
    }

    @Override
    public R removeOneById(Long id) {
            AuthorityLogEntity entity = new AuthorityLogEntity();
        entity.setId(id);
        entity.setIsDelete(1);
        boolean b = this.updateById(entity);
        if (!b){
            return R.error();
        }
        return R.ok();
    }

    @Override
    @Transactional
    public R removeByIdsE(List<Long> ids) {
        for (Long id : ids) {
            R r = removeOneById(id);
            if (!r.getFlag()){
                return r;
            }
        }
        return R.ok();
    }

}